package com.aaa.dao;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

public interface T_CyglDao {
    @Select("<script>SELECT i.name,e.id,e.begin_time,e.end_time,e.exam_name,e.score,e.number,e.one_score,e.exam_time,e.peopel,e.exam_state,e.information_id,i.email,i.number total FROM exam e,regist i " +
            " where e.information_id=i.id AND e.state=1 <if test=\"name!=null and name!=''\"> and exam_name like concat('%',#{name},'%')</if> " +
            " limit #{start},#{pageSize}</script>")
    List<Map> selectAll(Map map);

    @Select("<script>select count(*) from exam e, regist i " +
            " where e.information_id=i.id AND e.state=1 <if test=\"name!=null and name!=''\"> and exam_name like concat('%',#{name},'%')</if> </script>")
    Integer getCount(Map map);

    @Update("UPDATE exam SET state=0 where id=#{id}")
    Integer del(Integer id);

    @Select("SELECT i.id,i.name,e.information_id FROM  regist i left JOIN  (SELECT * FROM exam WHERE exam_state!='考试结束' and state=1) e on e.information_id=i.id WHERE   e.information_id is null ")
    List<Map> infoList();

    @Update("update exam set begin_time=#{begin_time},end_time=#{end_time},exam_name=#{exam_name},exam_time=#{exam_time},peopel=#{peopel} where id=#{id}")
    Integer upd(Map map);

    @Insert("insert into exam_people(username,password,exam_id) values(#{username},#{password},#{id})")
    int addExamPerson(Map map);

    @Select("SELECT COUNT(*) #{max} FROM t_questions WHERE questiontypeid=#{i}")
    Map max1(@Param("max") String max, @Param("i") int i);

   @Insert("insert into exam(begin_time,end_time,exam_name,score,number,one_score,exam_time,peopel,information_id) values(#{begin_time},#{end_time},#{exam_name},#{score},#{number},#{one_score},#{exam_time},#{peopel},#{information_id})")
    int add(Map map);

    /**
     * 根据类型随机选出题目
     * @param i 类型
     * @param a 题目数量
     * @return
     */
    @Select("Select * From t_questions where questiontypeid=#{i} Order By rand() Limit #{a}")
    List<Map> selQuestion(@Param("i") Integer i, @Param("a") Integer a);

    @Insert("insert into new_questions(question,optiona,optionb,optionc,answer,questiontypeid,year,exam_id) values(#{question},#{optiona},#{optionb},#{optionc},#{answer},#{questiontypeid},#{year},#{exam_id})")
    void addQuestion(Map map1);

    @Select("SELECT max(id) exam_id FROM exam")
    Map selExam_id();

    @Update("UPDATE exam SET exam_state='待考试' where id=#{id}")
    void updstate(Map map);

    @Update("UPDATE exam a INNER JOIN (SELECT id FROM exam WHERE begin_time=curdate()) b on a.id=b.id set a.exam_state='正在考试'")
    void begin();

    @Update("UPDATE exam a INNER JOIN (SELECT * FROM exam WHERE end_time=curdate()) b on a.id=b.id set a.exam_state='考试结束'")
    void end();
}
